<?php
class ClsNaanalCrypt 
{
    public $skey = "NaanalIsPowerful";
    public function __construct($key) 
    {
        $this->skey=$key;
    }
    public static function &getInstance($key)
    {
        static $arrCrypt=array();
        if(!isset($arrCrypt[$key]))
        {
            $arrCrypt[$key]=new ClsNaanalCrypt($key);
        }
        return $arrCrypt[$key];
    }
    
    private function encrypt($str, $pass)
    {
        $str_arr = str_split($str);
        $pass_arr = str_split($pass);
        $add = 0;
        $div = strlen($str) / strlen($pass);
        while($add <= $div)
        {
            $newpass .= $pass;
            $add++;
        }
        $pass_arr = str_split($newpass);
        foreach($str_arr as $key => $asc)
        {
            $pass_int = ord($pass_arr[$key]);
            $str_int = ord($asc);
            $int_add = $str_int + $pass_int;
            $ascii .= chr($int_add);
        }
        return $ascii;
    }
    
    function decrypt($enc, $pass)
    {
        $enc_arr = str_split($enc);
        $pass_arr = str_split($pass);
        $add = 0;
        $div = strlen($enc) / strlen($pass);
        while($add <= $div)
        {
            $newpass .= $pass;
            $add++;
        }
        $pass_arr = str_split($newpass);
        foreach($enc_arr as $key => $asc)
        {
            $pass_int = ord($pass_arr[$key]);
            $enc_int = ord($asc);
            $str_int = $enc_int - $pass_int;
            $ascii .= chr($str_int);
        }
        return $ascii;
    }
	
    public  function encode($value){ 
		
        if(!$value)
        {
            return false;
        }
        return trim($this->encrypt($value,$this->skey)); 
    }
    
    public function decode($value)
    {		
        if(!$value)
        {
            return false;
        }
        return trim($this->decrypt($value, $this->skey));
    }
}
?>